<?php

namespace app\common\model\orderdelivery;
use app\common\model\user\User;
use think\facade\Session;
use app\api\controller\Pub;
use think\Model;
class Disposalsite extends Model
{
     // 设置当前模型对应的完整数据表名称
        protected $table = 'disposalsite';
        protected $pk = 'DisposalsiteId';
    
        const STATEINT = 1;//创建状态
        const STATEOK = 1;//正常
        const STATENO = 2;//暂停
        const STATEDELETE = 404;//删除
    /**
     * 状态分类
     * @param  string $value [description]
     * @return [type]        [description]
     */
    public static function getStatusText($value = '')
    {
        $status = [-1=>'全部',1=>'正常合作',2=>'暂停合作'];
        if($value!==''){
            return $status[$value];
        }
        return $status;
    }

    /**
     * 创建处置单位信息
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function user_create($data){
        $userlogininfo = Pub::getuserid();
        $userid = $userlogininfo['msg'];
        $disp = new Disposalsite();
        $disp->DisposalSiteName = $data['DisposalSiteName'];
        $disp->UserName = $data['UserName'];
        $disp->UserTel = $data['UserTel'];
        $disp->State = Disposalsite::STATEINT;
        $disp->CreateTime = date('Y-m-d H:i',time());
        $disp->save();
        return $disp->DisposalsiteId;
    }

       /**
     * 获取订单列表
     * @param  [type] $data    [主数据]
     * @param  string $type    [查询类型/hawa/buss]
     * @param  [type] $is_page [是否分页显示]
     * @return [type]          [description]
     */
    public static function usergetlist($data,$type = '1',$is_page = true){
        $rdata = Disposalsite::where('State','<>',Disposalsite::STATEDELETE);
        $option = come_model_option($data,$rdata,$is_page);
        $rdata = $option['rdata'];
        $data = $option['data'];
  
        $rdata = $rdata->select();

        $rdata =  $rdata->hidden(['OperationLog']);

        $ndata['list'] = $rdata->toArray();
      
        $ndata['statetext'] = Disposalsite::getStatusText();
        if($is_page){
            $ndata['pageinfo'] = ['pageindex'=>$data['pageindex'],'pagemax'=>$data['count']];
        }
        return $ndata;
    }
 
    /**
     * 获取信息
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public static function user_getinfo($cid,$hidden=['OperationLog']){
        $data = Disposalsite::where('DisposalsiteId',$cid)->find();
        if(empty($data)){
            return false;
        }
        $data = $data->hidden($hidden);
        return $data;
    }

    /**
     * 用户修改企业信息
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function user_reinfo($data){
        if(empty($data['id'])){
            return moerr('缺少重要参数');
        }
        $info = Disposalsite::get($data['id']);
        $userinfo = User::getinfo();
        $reinfo = empty($info->OperationLog)?'':$info->OperationLog."\n";
        $reinfo = $reinfo .'【' .$userinfo->Name.'|'.$userinfo->UserId.'】在'.date('Y-m-d H:i',time()).'进行了信息调整：';
        $nreinfo = '';
        foreach ($data as $key => $value) {
            $sb = $info->getfiledname($key);
            if(empty($sb)){
                continue;
            }
            //取原数据
            $ovalue = $info->$key;
            if($key=='State'&&$value!==$ovalue){
                $info->$key = $value;
                //value将改变成文字所以提前赋值
                $value = $info->getStatusText((int)$value);
                $ovalue = $info->getStatusText((int)$ovalue);
                $nreinfo =  $nreinfo . '字段【'.$info->getfiledname($key).'】由【'.  $ovalue .'】修改为【' .$value.'】;'."\n";
            }else if(!empty($value)&&$ovalue !== $value){
                $nreinfo =  $nreinfo . '字段【'.$info->getfiledname($key).'】由【'.  $ovalue .'】修改为【' .$value.'】;'."\n";
                $info->$key = $value;
            }
        }
        if(!empty($nreinfo)){
            if($wtnote){
                $reinfo = $reinfo ."\n" . $nreinfo;
                $info->OperationLog = $reinfo;
            }
            $info->save();
        }else{
            return moerr('没有找到需要调整的数据');
        }
    }

    /**
     * 获取收集站名称
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function getsitelistname($data){
        if(empty($data)){
            return moerr('空数据');
        }
        $data = Disposalsite::where('DisposalsiteId','in',$data)->column('DisposalsiteId,DisposalSiteName');
        return $data;
    }

    public static function getfiledname($str,$type=1){
        $filename = ['DisposalSiteName'=>'处置场名称','UserName'=>'联系人',
                 'UserTel'=>'联系电话','State'=>'合作状态'];
        if(empty($filename[$str])){
            return false;
        }
        return $filename[$str];
    }
   

}
